package com.nny.alarm.edit;

import java.util.Calendar;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.text.InputType;
import android.text.Selection;
import android.text.Spannable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.nny.alarm.R;
import com.nny.alarm.edit.adapter.LabelAdapter;
import com.nny.alarm.more.AlarmBriefUtil;
import com.nny.alarm.widget.SlideDialog;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.skin.Skin;
import com.uxwine.util.UxToast;

public class LabelEditDialog extends SlideDialog implements 
		View.OnClickListener, 
		LabelAdapter.OnLabelClickedListener{

	private AlarmEditor mEditor;
	private EditText mEdit; 
	private LabelAdapter mNoteAdapter;
	private GridView mNotices;
	private TextView mtvTime;
	private TextView mtvCycle;
	private AlarmBriefUtil mAUtil;
	private Alarm mAlarm;
	private Typeface mTypeface;
	
	public LabelEditDialog(Context ctx) {
		super(ctx);
	}			
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dlg_alarm_name);
		setCanceledOnTouchOutside(true);
		
		final Context context = getContext();
		findViewById(R.id.alarm_name_ok).setOnClickListener(this);
		findViewById(R.id.alarm_name_cancel).setOnClickListener(this);
		mEdit = (EditText)findViewById(R.id.alarm_name_edit);
		mNotices = (GridView)findViewById(R.id.alarm_name_note);
		mtvTime = (TextView)findViewById(R.id.alarm_name_title_time);
		mtvCycle = (TextView)findViewById(R.id.alarm_name_title_cycle);
		
		mTypeface = Skin.getTypeface(context);
		mAUtil = AlarmBriefUtil.getInstance(context);
		mNoteAdapter = new LabelAdapter(context);
		
		mtvTime.setTypeface(mTypeface);
		mtvCycle.setTypeface(mTypeface);
		mNoteAdapter.setOnLabelClickedListener(this);
		mNotices.setAdapter(mNoteAdapter);

		mEdit.setFocusable(true);
		mEdit.setFocusableInTouchMode(true);
				
		final Resources res = getContext().getResources();
		String[] data = res.getStringArray(R.array.alarm_notice);
		mNoteAdapter.update(data);
		
		if(null != mEditor)
		{
			mEdit.setText(mEditor.mAlarm.label);
		}
		
		mAlarm = mEditor.build();
		Calendar trigger = calcTrigger(mAlarm);
		mtvTime.setText(mAUtil.getBaseTimeString(mAlarm, trigger));
		mtvCycle.setText(mAUtil.getCycleString(mAlarm));		
	}
	
	public void setEditor(AlarmEditor editor)
	{
		mEditor = editor;
	}	
	
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.alarm_name_ok)
		{
			if(mEditor != null)
			{
				String label = mEdit.getText().toString().trim();
				if(label == null || "".equals(label))
				{
					label = mEdit.getHint().toString().trim();
				}
				mEditor.mAlarm.label = label;
				exit(ACTION_OK);
			}
		}
		else if(id == R.id.alarm_name_cancel)
		{
			exit(ACTION_CANCEL);
		}
	}

	@Override
	public void onLabelClickedListener(View v, int id) {
		TextView tv = (TextView)v;
		if(setEditText(tv.getText().toString()))
			showSoftInput();
		else
			hideSoftInput();
	}
	
	public boolean setEditText(String text)
	{
		boolean bRet = false;
		int idx = text.indexOf("__");
		if(idx >= 0)
		{
			bRet = true;
			text = text.replace("__", "");
		}
		else
		{
			idx = text.length();
		}
		mEdit.setText(text);
		CharSequence cs = mEdit.getText();
		//Debug.asserts(text instanceof Spannable);
		if (cs instanceof Spannable) {
		     Spannable spanText = (Spannable)cs;
		     Selection.setSelection(spanText, idx);
		}
		return bRet;
	}
	
	public void showSoftInput()
	{
		InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);   
        imm.showSoftInput(mEdit, 0);
	}
	public void hideSoftInput()
	{
		mEdit.requestFocus();
		InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);   
        imm.hideSoftInputFromWindow(mEdit.getWindowToken(), 0); 
	}
	
	public Calendar calcTrigger(final Alarm alarm)
	{
		Calendar trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		if(alarm.cycle.isOnce())
		{
			trigger.setTimeInMillis(alarm.trigger);
		}
		return trigger;
	}
}
